<?php
/**
 * Created by PhpStorm.
 * User: wangye
 * Date: 19-2-14
 * Time: 下午6:33
 */

//指定服务端绑定id监听端口
$socket = stream_socket_server("tcp://0.0.0.0:8000",$erron,$errstr);#str,错误号,错误信息

//
if (!$socket){
    echo "$errstr($erron)\n";
}else{
    echo "socket 正常运行ing...\n";

    while (true){
        $client = stream_socket_accept($socket,-1);//时候程序与阻塞,阻塞超时时间-1永不超时
        if ($client){
            $data = fread($client,1024);//读数据1024个
            echo $data;
            fwrite($client,$data);//会写给客户端
        }
        fclose($client);
    }
    fclose($socket);
}